<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的购物车</title>
    <script src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        function modify(productId, obj) {

            /**
             * 参数一：选择器规则
             * 参数二：查找范围
             */
            var num = $('input[name=num]', $(obj).parent().parent()).val()
            // console.log(num)
            location.href = '${pageContext.request.contextPath}/modify.cart?id='+productId+'&num='+num
        }

        $(function () {
            $('#btnDelete').click(function(){
                $('#cartForm').submit() // 提交表单
            })
        })
    </script>
</head>
<body>
<h1>我的购物车</h1>
<hr/>
<form action="${pageContext.request.contextPath}/removeByIds.cart" method="get" id="cartForm">
<table border="1">
    <tr>
        <th>
            <input type="checkbox" id="all">全选
        </th>
        <th>序号</th>
        <th>商品名称</th>
        <th>商品数量</th>
        <th>商品总价</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${cart.items}" var="item" varStatus="status">
        <tr>
            <td>
                <input type="checkbox" name="productId" value="${item.product.id}"/>
            </td>
            <td>${status.index+1}</td>
            <td>${item.product.name}</td>
            <td>
                <input type="text" name="num" value="${item.num}"/>
            </td>
            <td>${item.price}</td>
            <td>
                <a href="javascript:;" onclick="modify(${item.product.id},this)">修改</a>
                <a href="${pageContext.request.contextPath}/removeById.cart?id=${item.product.id}" onclick="return confirm('是否确认删除')">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>
</form>
--------------------------------------------------------<br/>
总计:${cart.price}元<br/>
<a href="javascript:;" id="btnDelete">删除选中项</a>
<a href="${pageContext.request.contextPath}/clear.cart">清空购物车</a>
<a href="${pageContext.request.contextPath}/findAll.product">继续购物</a>
<a href="${pageContext.request.contextPath}/jsp/confirm.jsp">结算</a>
</body>
</html>